#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 20;
int n;
int a[N];
int t[N];
int judge(int a, int b)
{
	int count = 0;
	for (int i = 0; i < n; i++)
	{
		//if (((1 << i) & a) == 0 && ((1 << i) & b) == 1) count++;
		//if (((1 << i) & a) == 1 && ((1 << i) & b) == 0) return -1;
		if (((a >> i) & 1) == 0 && ((b >> i) & 1) == 1) count++;
		if (((a >> i) & 1) == 1 && ((b >> i) & 1) == 0) return -1;
	}
	return count;
}
int solve()
{
	int ret = 0x3f3f3f3f;
	for (int st = 0; st < (1 << n); st++)
	{
		memcpy(t, a, sizeof a);
		int change = st;
		int count = 0;
		int flg = 1;
		for (int i = 1; i <= n; i++)
		{
			int c = judge(t[i], change);
			if (!~c)
			{
				flg = 0; 
				break;
			}
			t[i] = change;
			count += c;
			change = t[i - 1] ^ (t[i] << 1) ^ (t[i] >> 1);
			change &= ((1 << n) - 1);
		}
		if (flg) ret = min(ret, count);
	}
	if (ret != 0x3f3f3f3f) return ret;
	else return -1;
}
int main()
{
	int T; cin >> T;
	for (int i = 0; i < T; i++)
	{
		int count = 0;
		memset(a, 0, sizeof a);
		cin >> n;
		for (int j = 1; j <= n; j++)
		{
			for (int k = 0; k < n; k++)
			{
				int tmp; cin >> tmp;
				if (tmp) a[j] |= (1 << k);
			}
		}
		count += solve();
		printf("Case %d: %d\n", i + 1, count);
	}
	return 0;
}